﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;

public partial class Main_ServiceInvoice_ServiceCategory : System.Web.UI.Page
{
    Datautility du = new Datautility();
    string sql = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridCategory();
        }
    }

    protected void InsertCategory()
    {
        try
        {
            string itemcategory_name = TxtCategoryName.Text;
            string sql = "select SERVICECATEGORYID from IVC_SERVICECATEGORY where SERVICECATEGORY = '" + itemcategory_name + "'";

            DataTable DT = du.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Category already exists! ";
            }
            else
            {
                SqlParameter[] parm = {
                                  new SqlParameter("@SERVICECATEGORY", TxtCategoryName.Text.Trim())
                              };
                du.getdatausingprocedure("usp_Insert_IVC_SERVICECATEGORY", parm);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Service Category Created";
                TxtCategoryName.Text = string.Empty;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void BindGridCategory()
    {
        sql = "select SERVICECATEGORYID, SERVICECATEGORY from IVC_SERVICECATEGORY";
        DataTable dt = du.getdata(sql);
        GrdCategory.DataSource = dt;
        GrdCategory.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                InsertCategory();
                scope.Complete();
            }
            BindGridCategory();
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
     
    }
    protected void GrdCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GrdCategory.EditIndex = e.NewEditIndex;
        BindGridCategory();
    }
    protected void GrdCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrdCategory.EditIndex = -1;
        BindGridCategory();
    }
    protected void GrdCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox Txtname = (TextBox)GrdCategory.Rows[e.RowIndex].Cells[1].Controls[0];
        String Key = GrdCategory.DataKeys[e.RowIndex].Value.ToString();
        if (Txtname.Text != string.Empty)
        {
            string sql = "Select SERVICECATEGORYID from IVC_SERVICECATEGORY where SERVICECATEGORY ='"+Txtname.Text+"'" ;
            DataTable dt = du .getdata(sql);
            if (dt.Rows.Count > 0 )
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Category already exists! ";
            }
            else
            {
                SqlParameter[] parm ={
                                        new SqlParameter("SERVICECATEGORY",Txtname.Text.Trim()),
                                        new SqlParameter("SERVICECATEGORYID",GrdCategory.DataKeys[e.RowIndex].Value.ToString())
                                      };
                du.executeprocedure("USP_IVC_Update_SERVICECATEGORY  ", parm);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Category details updated successfully...";
                GrdCategory.EditIndex = -1;
                BindGridCategory();
            }
        }
    }
 
}
